<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :model-value="baTable.form.operate ? true : false" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    class="flex"
                >
                    <FormItem
                        :label="t('xCertificate.CORP')"
                        type="string"
                        v-model="baTable.form.items!.CORP"
                        prop="CORP"
                        :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.CORP') }), disabled: true }"
                    />
                    <FormItem
                        :label="t('xCertificate.ZJNAME')"
                        type="string"
                        v-model="baTable.form.items!.ZJNAME"
                        prop="ZJNAME"
                        :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.ZJNAME') }) }"
                    />
                    <!-- <FormItem
                        :label="t('xCertificate.ZJNAME')"
                        type="select"
                        v-model="baTable.form.items!.ZJNAME"
                        :data="{ content: props.formData.controllerList }"
                        :placeholder="t('security.dataRecycle.The data collection mechanism will monitor delete operations under this controller')"
                    /> -->
                    <!-- <FormItem :label="t('xCertificate.ZJTYPE')" type="string" v-model="baTable.form.items!.ZJTYPE" prop="ZJTYPE" :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.ZJTYPE') }) }" /> -->
                    <FormItem
                        :label="t('xCertificate.FZJG')"
                        type="string"
                        v-model="baTable.form.items!.FZJG"
                        prop="FZJG"
                        :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.FZJG') }) }"
                    />
                    <FormItem
                        :label="t('xCertificate.FZRQ')"
                        type="datetime"
                        v-model="baTable.form.items!.FZRQ"
                        prop="FZRQ"
                        :input-attr="{ placeholder: t('Please select field', { field: t('xCertificate.FZRQ') }) }"
                    />
                    <FormItem
                        :label="t('xCertificate.YXQDATE')"
                        type="datetime"
                        v-model="baTable.form.items!.YXQDATE"
                        prop="YXQDATE"
                        :input-attr="{ placeholder: t('Please select field', { field: t('xCertificate.YXQDATE') }) }"
                    />
                    <FormItem
                        :label="t('xCertificate.RANK')"
                        type="string"
                        v-model="baTable.form.items!.RANK"
                        prop="RANK"
                        :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.RANK') }) }"
                    />
                    <FormItem
                        style="width:100%"
                        :label="t('xCertificate.FILENAME')"
                        type="file"
                        v-model="baTable.form.items!.FILENAME"
                        prop="FILENAME"
                        :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.FILENAME') }) }"
                    />
                    <FormItem
                        style="width:100%"
                        :label="t('xCertificate.DEMO')"
                        type="textarea"
                        v-model="baTable.form.items!.DEMO"
                        prop="DEMO"
                        :input-attr="{ rows: 3, placeholder: t('Please input field', { field: t('xCertificate.DEMO') }) }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                    />
                    <FormItem
                        :label="t('xCertificate.OP')"
                        type="string"
                        v-model="baTable.form.items!.OP"
                        prop="OP"
                        :input-attr="{ placeholder: t('Please input field', { field: t('xCertificate.OP') }),disabled:true }"
                    />
                    <FormItem
                        :label="t('xCertificate.OPTIME')"
                        type="datetime"
                        v-model="baTable.form.items!.OPTIME"
                        prop="OPTIME"
                        :input-attr="{ placeholder: t('Please select field', { field: t('xCertificate.OPTIME') }),disabled:true }"
                    />
                    
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject ,watchEffect} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { useRoute } from 'vue-router'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    FZRQ: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('xCertificate.FZRQ') }) })],
    YXQDATE: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('xCertificate.YXQDATE') }) })],
    OPTIME: [buildValidatorData({ name: 'date', message: t('Please enter the correct field', { field: t('xCertificate.OPTIME') }) })],
})

const route = useRoute()
const type = route.name === 'corplib/customer'? 1:2

const companyName = inject('companyName') as String
watchEffect(()=>{
    if(baTable.form.operate == 'add'){
        baTable.form.items!.CORP = companyName.value
        baTable.form.items!.VALIDORNOT = '1'
    }
})

const handleSave= ()=>{
    baTable.form.items!.type = type
    baTable.onSubmit(formRef.value)
}


</script>

<style scoped lang="scss"></style>
